/*
Copyright 2010 Sieric
http://sieric.ca

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testing;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import junit.framework.Assert;
import savior.fs.FSShouldRetryException;
import savior.fs.FileDescription;
import savior.fs.FTP;

public class Tools {

    static public void assertFDLists(List<FileDescription> expected, List<FileDescription> result) {
        Assert.assertEquals(expected.size(), result.size());

        for (FileDescription e : expected) {
            boolean found = false;
            for (FileDescription r : result) {
                if (e.name.equals(r.name)) {
                    Assert.assertEquals(e.size, r.size);
                    if (e.time == -1) {
                        long current = new Date().getTime();
                        Assert.assertTrue( (r.time > (current - 3600000)) && (r.time < (current + 3600000)) );
                    } else {
                        Assert.assertEquals(e.time, r.time);
                    }
                    found = true;
                    break;
                }
            }

            if (!found) {
                Assert.fail("Did not find " + e.name);
            }
        }
    }

    static public void assertInputStream(InputStream expected, InputStream result) throws IOException {
        byte[] b = new byte[1024];
        byte[] b2 = new byte[1024];
        int lenExp, lenRes;
        while (((lenExp = expected.read(b)) != -1) && ((lenRes = result.read(b2)) != -1)) {
            Assert.assertEquals(lenExp, lenRes);
            for (int i = 0; i < lenExp; ++i) {
                Assert.assertEquals(b[i], b2[i]);
            }
        }
    }

    static public void assertStringLists(List<String> expected, List<String> result) {
        Assert.assertEquals(expected.size(), result.size());

        for (String e : expected) {
            boolean found = false;
            for (String r : result) {
                if (e.equals(r)) {
                    found = true;
                    break;
                }
            }

            if (!found) {
                Assert.fail("Did not find " + e);
            }
        }
    }

    static public void copy(InputStream in, OutputStream out) throws IOException {
        byte[] b = new byte[1024];
        int len;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
    }

    static public boolean createDirectory(String directory, String dirname) {
        File file = new File(directory + File.separatorChar + dirname);
        return file.mkdir();
    }

    static public boolean createTextFile(String directory, String filename, String content) {
        File file = new File(directory + File.separatorChar + filename);
        try {
            PrintWriter pw = new PrintWriter(file);
            pw.print(content);
            pw.close();
        } catch (FileNotFoundException ex) {
            return false;
        }

        return true;
    }

    static public String createTextFileFTP(String directory, String filename, String content, FTP instance) throws IOException, FSShouldRetryException {
        // Create a file locally
        String localDirectory = createTempDir();
        Tools.createTextFile(localDirectory, filename, content);

        // Put the file on the FTP
        String localFileName = localDirectory + File.separatorChar + filename;
        InputStream in = new FileInputStream(localFileName);
        OutputStream out = instance.putFile(directory + filename, false);
        Tools.copy(in, out);
        in.close();
        out.close();

        return localFileName;
    }

    static public String createTempDir() throws IOException {
        File file = File.createTempFile("junit", "");
        file.delete();
        file.mkdir();
        return file.getAbsolutePath();
    }
}
